<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Shop\Cart;
use App\Models\Shop\Address;
use App\Models\Shop\Order;
use JWTAuth;
use DB;

class OrderController extends Controller
{
    public function check_out()
    {
        $user = JWTAuth::parseToken()->authenticate();
//        return $user;

        $carts = Cart::with('product')->where('customer_id', $user->id)->get();
        $count = Cart::count_cart($carts);

        //如果购物车中没有商品,跳回购物车页面
//        if ($carts->isEmpty()) {
//            return redirect('/cart');
//        }
        $address = Address::find($user->address_id);
        return response()->json(compact('carts', 'count', 'address'));

    }

    public function show_pay()
    {
        $user = JWTAuth::parseToken()->authenticate();

        $data = [];
        $count = Cart::count_cart();
        $total_price = $count['total_price'];

//        $car = Cart::where('customer_id', 'wechat.customer.id')->get();



        DB::beginTransaction();
        try {
            //生成订单
            $order = Order::create([
                'user_id' => $user->id,
                'total_price' => $total_price,
                'status' => 1,
            ]);
            //订单地址
            $address = Address::find($user->address_id);
            $order->address()->create([
                'province' => $address['province'],
                'city' => $address['city'],
                'area' => $address['area'],
                'detail' => $address['detail'],
                'tel' => $address['tel'],
                'name' => $address['name']
            ]);
            $carts = Cart::with('product')->where('customer_id', $user->id)->get();
//            if ($carts->isEmpty()) {
//                $data['status'] = 0;
//                $data['info'] = '当前订单已提交';
//                return $data;
//            }
            foreach ($carts as $cart) {
                //判断库存是否充足
                if ($cart->product->stock != '-1' and $cart->product->stock - $cart->num < 0) {
                    throw new \Exception('商品' . $cart->product->name . ",目前仅剩下" . $cart->product->stock .
                        '件。\n请返回购物车，修订订单后再下单');
                }
                //消减库存数量
                if ($cart->product->stock != '-1') {
                    $cart->product->decrement('stock', $cart->num);
                }
                //插入订单商品表
                $order->order_products()->create([
                    'product_id' => $cart->product_id,
                    'num' => $cart->num,
                ]);

            }
            //清空购物车
            Cart::with('product')->where('customer_id', $user->id)->delete();
        } catch (\Exception $e) {
            DB::rollback();
            $data['status'] = 0;
            $data['info'] = $e->getMessage();
//            return $data;

        }
        DB::commit();

        $data['status'] = 1;
        $data['order_id'] = $order->id;
//        return $data;

        return response()->json(compact('order',  'address'));


    }

}
